﻿
namespace Pixels.Core
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;

    public static class CustomAttributeHelper
    {
        public static bool HasCustomAttribute(object objClass, string Name)
        {
            bool has = false;
            if (objClass.GetType().GetCustomAttributesData().Count > 0)
            {
                for (int i = 0; i < objClass.GetType().GetCustomAttributesData().Count; i++)
                {
                    if (objClass.GetType().GetCustomAttributesData()[i].ToString().ToLower().Contains(Name.ToLower()))
                    {
                        has = true;
                        break;
                    }
                }
            }
            return has;
        }

        public static bool HasCustomAttribute(this PropertyInfo property, string Name)
        {
            bool has = false;
            if (property.GetCustomAttributesData().Count > 0)
            {
                for (int i = 0; i < property.GetCustomAttributesData().Count; i++)
                {
                    if (property.GetCustomAttributesData()[i].ToString().ToLower().Contains(Name.ToLower()))
                    {
                        has = true;
                        break;
                    }
                }
            }
            return has;
        }

        public static string GetCustomAttributeConstructorValue(this PropertyInfo property, string AttributeName, int index)
        {
            string attr = "";
            IList<CustomAttributeData> _cusattributedatas = property.GetCustomAttributesData();

            if (_cusattributedatas.Count > 0)
            {
                for (int i = 0; i < _cusattributedatas.Count; i++)
                {
                    CustomAttributeData _cusattributedata = _cusattributedatas[i];

                    if (_cusattributedata.ToString().ToLower().Contains(AttributeName.ToLower()))
                    {
                        if (_cusattributedata.ConstructorArguments.Count > 0)
                            if (_cusattributedata.ConstructorArguments[index] != null)
                            {
                                attr = _cusattributedata.ConstructorArguments[index].Value.ToString();
                                break;
                            }
                    }
                }
            }
            return attr;
        }

        public static string GetCustomAttributeConstructorValue(object objClass, string AttributeName, int index)
        {
            string attr = "";
            IList<CustomAttributeData> _cusattributedatas = objClass.GetType().GetCustomAttributesData();

            if (_cusattributedatas.Count > 0)
            {
                for (int i = 0; i < _cusattributedatas.Count; i++)
                {
                    CustomAttributeData _cusattributedata = _cusattributedatas[i];

                    if (_cusattributedata.ToString().ToLower().Contains(AttributeName.ToLower()))
                    {
                        if (_cusattributedata.ConstructorArguments.Count > 0)
                            if (_cusattributedata.ConstructorArguments[index] != null)
                            {
                                attr = _cusattributedata.ConstructorArguments[index].Value.ToString();
                                break;
                            }
                    }
                }
            }
            return attr;
        }

        public static string GetCustomAttributeNamedValue(this PropertyInfo property, string AttributeName, string Name)
        {
            string attr = "";
            IList<CustomAttributeData> _cusattributedatas = property.GetCustomAttributesData();

            if (_cusattributedatas.Count > 0)
            {
                for (int i = 0; i < _cusattributedatas.Count; i++)
                {
                    CustomAttributeData _cusattributedata = _cusattributedatas[i];

                    if (_cusattributedata.ToString().ToLower().Contains(AttributeName.ToLower()))
                    {
                        if (_cusattributedata.NamedArguments.Count > 0)
                        {
                            CustomAttributeNamedArgument arg = _cusattributedata.NamedArguments.Where(p => p.MemberInfo.Name.ToLower() == Name.ToLower()).FirstOrDefault();
                            if (arg != null)
                            {
                                attr = arg.TypedValue.Value.ToString();
                            }
                        }
                                                           
                    }
                }
            }
            return attr;
        }       
    }
}
